import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HorseRacingGame extends JFrame {
    private static final int WIDTH = 800;
    private static final int HEIGHT = 500;
    private static final int HORSE_COUNT = 5;
    private static final int FINISH_LINE = WIDTH - 100;

    private List<Horse> horses;
    private JPanel racePanel;
    private JButton startButton;
    private JButton resetButton;
    private JLabel resultLabel;
    private boolean isRacing;
    private Timer raceTimer;

    // 马匹类
    class Horse {
        String name;
        Color color;
        int position;
        int yPosition;

        public Horse(String name, Color color, int yPosition) {
            this.name = name;
            this.color = color;
            this.yPosition = yPosition;
            this.position = 0;
        }

        public void move() {
            Random random = new Random();
            position += random.nextInt(5) + 1; // 随机前进1-5像素
        }
    }

    public HorseRacingGame() {
        initializeUI();
        initializeHorses();
    }

    // 初始化界面
    private void initializeUI() {
        setTitle("Java赛马小游戏");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 比赛面板
        racePanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                drawRaceTrack(g);
                drawHorses(g);
            }
        };
        racePanel.setBackground(Color.WHITE);
        add(racePanel, BorderLayout.CENTER);

        // 控制面板
        JPanel controlPanel = new JPanel();
        startButton = new JButton("开始赛马");
        resetButton = new JButton("重置");
        resultLabel = new JLabel("准备开始比赛！");

        startButton.addActionListener(new StartRaceListener());
        resetButton.addActionListener(new ResetRaceListener());

        controlPanel.add(startButton);
        controlPanel.add(resetButton);
        controlPanel.add(resultLabel);
        add(controlPanel, BorderLayout.SOUTH);

        // 定时器控制赛马动画
        raceTimer = new Timer(50, new RaceTimerListener());
    }

    // 初始化马匹
    private void initializeHorses() {
        horses = new ArrayList<>();
        String[] horseNames = {"赤兔", "的卢", "绝影", "爪黄飞电", "照夜玉狮子"};
        Color[] horseColors = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.MAGENTA};
        int yPos = 50;

        for (int i = 0; i < HORSE_COUNT; i++) {
            horses.add(new Horse(horseNames[i], horseColors[i], yPos));
            yPos += 80;
        }
    }

    // 绘制赛道
    private void drawRaceTrack(Graphics g) {
        // 终点线
        g.setColor(Color.RED);
        g.drawLine(FINISH_LINE, 20, FINISH_LINE, HEIGHT - 100);
        g.drawString("终点线", FINISH_LINE - 40, HEIGHT - 80);

        // 赛道分隔线
        for (int i = 0; i <= HORSE_COUNT; i++) {
            int y = 30 + i * 80;
            g.setColor(Color.LIGHT_GRAY);
            g.drawLine(50, y, WIDTH - 50, y);
        }
    }

    // 绘制马匹
    private void drawHorses(Graphics g) {
        for (Horse horse : horses) {
            // 绘制马匹主体
            g.setColor(horse.color);
            g.fillRect(horse.position + 50, horse.yPosition - 20, 60, 30);
            
            // 绘制马匹头部
            g.fillOval(horse.position + 110, horse.yPosition - 15, 20, 20);
            
            // 绘制马匹名称
            g.setColor(Color.BLACK);
            g.drawString(horse.name, horse.position + 55, horse.yPosition - 25);
        }
    }

    // 开始比赛监听器
    private class StartRaceListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!isRacing) {
                isRacing = true;
                resultLabel.setText("比赛进行中...");
                startButton.setEnabled(false);
                raceTimer.start();
            }
        }
    }

    // 重置比赛监听器
    private class ResetRaceListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            isRacing = false;
            raceTimer.stop();
            for (Horse horse : horses) {
                horse.position = 0;
            }
            resultLabel.setText("准备开始比赛！");
            startButton.setEnabled(true);
            racePanel.repaint();
        }
    }

    // 比赛定时器监听器
    private class RaceTimerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            boolean raceFinished = false;
            Horse winner = null;

            for (Horse horse : horses) {
                horse.move();
                
                // 检查是否到达终点
                if (horse.position >= FINISH_LINE - 70 && !raceFinished) {
                    raceFinished = true;
                    winner = horse;
                }
            }

            racePanel.repaint();

            // 比赛结束处理
            if (raceFinished) {
                raceTimer.stop();
                isRacing = false;
                resultLabel.setText("比赛结束！冠军：" + winner.name + "！");
                startButton.setEnabled(true);
                JOptionPane.showMessageDialog(null, winner.name + "获得胜利！", "比赛结果", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            HorseRacingGame game = new HorseRacingGame();
            game.setVisible(true);
        });
    }
}